//
//  RobeDetailsViewController.m
//  WeddingStoreFinder
//
//  Created by Macdev on 11/20/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "RobeDetailsViewController.h"
#import "POI.h"
#import "AppDelegate.h"
#import "MapViewController.h"
#import "ZoomLevel.h"
#import "Utils.h"

@interface RobeDetailsViewController()

@property (nonatomic, retain) ImageLoader *imageLoader;

@end

@implementation RobeDetailsViewController
@synthesize dressImageView;
@synthesize descLabel;
@synthesize imageLoaderIndicator;
@synthesize availableLabel;
@synthesize amountLabel;
@synthesize dress, imageLoader;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.title = dress.poi.poiName;
	
	descLabel.text = dress.dressDesc;
	if ([dress.isAvailable intValue] == kBoolIntValue_True) {
		self.availableLabel.textColor = [UIColor greenColor];
		self.availableLabel.text = @"Available";
	} else {
		self.availableLabel.textColor = [UIColor redColor];
		self.availableLabel.text = @"Not available";
	}
	self.amountLabel.text = [Utils formatCurrency:[dress.price intValue]];
	
	ImageLoader *il = [[ImageLoader alloc] init];
	self.imageLoader = il;
	[il release];
	imageLoader.delegate = self;
	imageLoader.imageName = dress.imageName;
	[imageLoader imageLoaded];
	[imageLoaderIndicator startAnimating];
	
}


- (void)viewDidUnload
{
    [self setDressImageView:nil];
    [self setDescLabel:nil];
    [self setImageLoaderIndicator:nil];
    [self setAvailableLabel:nil];
	[self setAmountLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
	imageLoader.delegate = nil;
    [dressImageView release];
    [descLabel release];
    [imageLoaderIndicator release];
    [availableLabel release];
	[dress release];
	[amountLabel release];
    [super dealloc];
}

#pragma mark - IBAction

- (IBAction)showMapLabelPressed:(id)sender {
	AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
	MapViewController *mapViewController = appDelegate.mapViewController;
	UITabBarController *tabCont = appDelegate.tabbarController;
	if (tabCont.selectedIndex != 0) {
		tabCont.selectedIndex = 0;
	}
	[mapViewController.navigationController popToRootViewControllerAnimated:NO];
	CLLocationCoordinate2D poiPoint;
	poiPoint.longitude = [dress.poi.longitude doubleValue];
	poiPoint.latitude = [dress.poi.latitude doubleValue];
	[self.navigationController popViewControllerAnimated:NO];
	[mapViewController.mapView setCenterCoordinate:poiPoint zoomLevel:15 animated:YES];
	[mapViewController.mapView selectAnnotation:dress.poi animated:YES];
}

#pragma mark - ImageLoaderDelegate

- (void)imageDidLoad:(ImageLoader*)_imageLoader {
	[self.imageLoaderIndicator stopAnimating];
	self.dressImageView.image = _imageLoader.image;
}

- (void)noImageFound:(ImageLoader*)_imageLoader {
	NSLog(@"image not found: %@", imageLoader.imgURL);
	[self.imageLoaderIndicator stopAnimating];
	
}
@end
